package cn.spike.rockcc.mq.consumer;

import cn.spike.rockcc.domain.CourseOrder;
import cn.spike.rockcc.domain.CourseOrderItem;
import cn.spike.rockcc.errorenum.GlobalExceptionCode;
import cn.spike.rockcc.service.ICourseOrderItemService;
import cn.spike.rockcc.service.ICourseOrderService;
import cn.spike.rockcc.util.AssertUtil;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


/**
 * @author:spike
 * @create:2023-03-23 15:24
 */
@RocketMQMessageListener(consumerGroup = "service-consumer-courseOrder",
        topic = "order-delay-topic",
        selectorExpression = "order-delay-tags",
        messageModel = MessageModel.BROADCASTING
)
@Component
public class CourseOrderDelayConsumer implements RocketMQListener<String> {

    @Autowired
    private ICourseOrderService courseOrderService;

    @Autowired
    private ICourseOrderItemService courseOrderItemService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void onMessage(String orderNo) {
        CourseOrder courseOrder = courseOrderService.selectByOrderNo(orderNo);
        AssertUtil.isNotNull(courseOrder, GlobalExceptionCode.ORDER_CONSUME_EXCEPTION);
        //如果订单的状态不是支付完成
        if(!courseOrder.getStatusOrder().equals(CourseOrder.ORDER_PAY_SUCCESS))
            courseOrderService.orderTimeOut(courseOrder);
        CourseOrderItem courseOrderItem = courseOrderItemService.getCourseOrderItem(courseOrder.getId());
        if(courseOrderItem!=null && courseOrderItem.getKillCourseId() != null)
            redisTemplate.delete(orderNo);
    }
}
